<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="dateRange" label="挂号日期" class="w-[330px]">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="dateRange" label="" class="ml-2">
          <a-input v-model:value="formState.pyjm" placeholder="患者姓名/助记码" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="absolute top-[-9999px] hidden">
        <a-button ref="printMe" v-print="{ id: 'printMe' }">打印</a-button>
        <PrintView id="printMe" :print-data="printData" />
      </div>
      <BaseTable :loading="loading" column-code="001157" fill :data-source="tableData" bordered :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex == 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onPrintInpCert(record)"> 住院证 </a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
import { getSystemConfig } from '@/utils/systemConfig'
import SearchContainer from '@/components/SearchContainer'

const isArmy = getSystemConfig('NAVYHIS_MODE') === '1' //军用系统
const printMe = ref()
const printData = ref({})
const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().subtract(20, 'day').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  pyjm: ''
})
const onPrintInpCert = async (record) => {
  // 输入型0:通用|1：住院证
  if (getSystemConfig('INP_NOTICE_DOCUMENT_MODE') === '1') {
    const { code, msg, data } = await post('/inp/Print/hospitalizationCertificatePrint', { jzbh: record.keycode })
    if (code !== 0) return message.error(msg)
    window.openExe?.rpt(data) //调用winform打印
  } else {
    const { code, msg, data } = await post('/inp/Print/hospitalizationNoticesPrint', { jzbh: record.keycode })
    if (code !== 0) return message.error(msg)
    if (isArmy) {
      printData.value = JSON.parse(data)
      setTimeout(() => {
        printMe.value.$el.click()
      }, 1000)
    } else {
      window.openExe?.rpt(data) //调用winform打印
    }
  }
}

const { loading, tableData, pagination, getData } = useTable({
  url: '/inp/zydj/getInpAppointmentRecordList',
  immediate: true,
  params: formState.value,
  formatParams: (params) => {
    return {
      ...params,
      stdate: params['dateRange']?.[0],
      eddate: params['dateRange']?.[1]
    }
  }
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
